package cn.lanqiao.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.lanqiao.entity.Exam_manage;
import cn.lanqiao.entity.Near;
import cn.lanqiao.entity.Pages;
import cn.lanqiao.service.Exam_manageService;
/**
 * 考试管理
 * @author yejun
 */
@Service
public class Exam_manageServiceImpl implements Exam_manageService {

	@Autowired
	private cn.lanqiao.mapper.Exam_manageMapper exam_manageMapper;


	@Override
	public List<Exam_manage> getAllExam() {
		return exam_manageMapper.listAllExam();
	}

	//分页
	@Override
	public Pages<Exam_manage> ExamPage(int indexPage, int pageSize, String searchkey) {
		Pages<Exam_manage> page = new Pages<Exam_manage>();
		//当前页面indexPage
		page.setIndexPage(indexPage);
		//总记录数totalData
		int totalData = exam_manageMapper.count(searchkey);
		page.setTotalData(totalData);
		//当前页面记录数pageSize;
		page.setPageSize(pageSize);
		//总页数totalPages
		int totalPages = totalData % pageSize ==0 ? totalData / pageSize : totalData/ pageSize + 1;
		page.setTotalPages(totalPages);
		
		Map<String,Object> map = new HashMap<String,Object>();
		int startindex = (indexPage -1) * pageSize;
		int endindex = pageSize;
		
		map.put("startindex", startindex);
		map.put("endindex", endindex);
		map.put("searchkey", searchkey);
		
		List<Exam_manage> data = exam_manageMapper.ExamPages(map);
		page.setPageData(data);
		
		return page;
	}

	//查看详情
	@Override
	public Exam_manage getExamByCode(Integer examCode) {
		return exam_manageMapper.getExam(examCode);
	}

	//删除
	@Override
	public int deleteExam(Integer examCode  ) {
		return exam_manageMapper.delete(examCode);
	}

	//修改
	@Override
	public int updateExam(Exam_manage exam) {
		return exam_manageMapper.update(exam);
	}

	//添加
	@Override
	public int insertExam(Exam_manage exam) {
		return exam_manageMapper.insert(exam);
	}

	@Override
	public List<Near> nearAllExam( int day ) {
		List<Near> listNear = new ArrayList<Near>();
		List<Exam_manage> list = exam_manageMapper.nearExam();
		Date nowDate = new Date();
		for( Exam_manage datelist :  list ) {
			SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
			String element = datelist.getExamDate();
			Date date1 = null ;
			try {
				date1 = formatDate.parse(element);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			long daysBetween=(date1.getTime()-nowDate.getTime()+1000000)/(60*60*24*1000);
			if( 0 < daysBetween && daysBetween < day ) {
				Near near = new Near();
				near.setDaysBetween(daysBetween);
				near.setExamDate(element);
				near.setSource(datelist.getSource());
				listNear.add(near);
			}
		}
		Collections.sort(listNear);
		return listNear;
	}
    @Override
    public List<Exam_manage> findAll() {
        return exam_manageMapper.findAll();
    }

    @Override
    public IPage<Exam_manage> findAll(Page<Exam_manage> page) {
        return exam_manageMapper.findAll(page);
    }

   
    
    @Override
    public Exam_manage findOnlyPaperId() {
        return exam_manageMapper.findOnlyPaperId();
    }

	@Override
	public Exam_manage findBySource(String source) {
		
		return exam_manageMapper.findBySource(source);
	}

	
}
